package com.example.yls.demoa;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final int GET_IMG = 2001;
    private static final String TAG = "MainActivity";
    private Button btn_RequestUrl;
private Button btn_RequestImg;
private ImageView img_v;
private Handler handler;
private RequestQueue requestQueue;
private Button btn_imgLoader;
private myLoaderCache myLoaderCache;
private Button btn_net;
private NetworkImageView net_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBundler();
        btn_RequestUrlEvent();
        btn_RequestImgEvent();
        btn_imgLoaderEvent();
        btn_netEvent();
    }
    private void btn_netEvent() {
                btn_net.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String url="http://jsxy.gdcp.cn/UploadFile/2/2019/3/15/2019315193634465.png";
                        ImageLoader imageLoader=new ImageLoader(requestQueue,myLoaderCache);
                        net_img.setDefaultImageResId(R.drawable.def);
                        net_img.setErrorImageResId(R.drawable.err);
                        net_img.setImageUrl(url,imageLoader);
                    }
                });
    }
    private void btn_imgLoaderEvent() {
                     btn_imgLoader.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View view) {
                             String url="http://jsxy.gdcp.cn/UploadFile/2/2019/3/15/2019315193634465.png";
                             ImageLoader imageLoader=new ImageLoader(requestQueue,myLoaderCache);
                             ImageLoader.ImageListener imageListener=
                                     ImageLoader.getImageListener(img_v,R.drawable.def,R.drawable.err);
                          imageLoader.get(url,imageListener);
                         }
                     });
    }
    private void btn_RequestImgEvent() {
                    btn_RequestImg.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                   String imgv="http://jsxy.gdcp.cn/UploadFile/2/2019/3/15/2019315193634465.png";
                            ImageRequest imageRequest=new ImageRequest
                                    (imgv, new Response.Listener<Bitmap>() {
                                @Override
                                public void onResponse(Bitmap bitmap) {
                            img_v.setImageBitmap(bitmap);
                                    Log.e(TAG, "onResponse: " );
                                }
                            }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError volleyError) {

                                }
                            });
                            requestQueue.add(imageRequest);

                        }
                    });
    }
    private void btn_RequestUrlEvent() {
        btn_RequestUrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String s_img="http://www.pptbz.com/pptpic/UploadFile" +
                                "s_6909/201203/2012031220134655.jpg";
                        try {
                            URL url=new URL(s_img);
                            HttpURLConnection httpURLConnection=
                                    (HttpURLConnection) url.openConnection();
                            InputStream inputStream=httpURLConnection.getInputStream();
                            Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
                            Message message= Message.obtain();
                            message.what=GET_IMG;
                            message.obj=bitmap;
                            handler.sendMessage(message);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
    private void initBundler() {
        myLoaderCache=new myLoaderCache();
        btn_imgLoader=findViewById(R.id.btn_imgLoader);
                  handler=new Handler(new Handler.Callback() {
                      @Override
                      public boolean handleMessage(Message message) {
                          if (message.what==GET_IMG){
                              Bitmap bitmap= (Bitmap) message.obj;
                              img_v.setImageBitmap(bitmap);
                          }
                          return false;
                      }
                  });
                 btn_RequestUrl=findViewById(R.id.btn_url);
                 btn_RequestImg=findViewById(R.id.btn_imgRequest);
                 img_v=findViewById(R.id.iv_img);
                 requestQueue= Volley.newRequestQueue(this);
                 btn_net=findViewById(R.id.btn_net);
                 net_img=findViewById(R.id.net_imgview);
    }
}
